﻿using System.Collections.Generic;
using System.ComponentModel;
using SQLServerHealthCheck.Model;
using System.Linq;
using System.Windows.Media;
using System.Collections;
using AG.Utilities;
namespace SQLServerOptimizer.PL.WPF{


    public class IssueViewModel : INotifyPropertyChanged

    {
        #region Data

        bool? _isChecked = false;
        IssueViewModel _parent;

        #endregion // Data

        #region Issues

        public static List<IssueViewModel> CreateIssues()
        {

            IssueViewModel root = new IssueViewModel("All", new SolidColorBrush(Colors.Blue));
          
            try
            {
               
                   
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"].ToString());

                var servers = from s in dc.Servers
                              select s;
                if (AutoFixControl.expandlevel > -1)
                {
                    root.IsInitiallySelected = true;
                }
              
                  root.IsNodeExpanded = true;
                foreach (var serve in servers)
                {


                    IssueViewModel servernode = new IssueViewModel("Server: " + serve.Name, new SolidColorBrush(Colors.Blue));
                    if (AutoFixControl.expandlevel > -1)
                    {
                        servernode.IsNodeExpanded = true; 
                    }
                   

                    var services = from i in serve.Services
                                   where i.ServiceTypeID == 1
                                   &&( AutoFixControl.serviceId == i.ServiceID || AutoFixControl.serviceId ==-1)
                                   select i;
                    var serverissues = from si in dc.Issues
                                       where (si.IssueType.Scope.ScopeId == 1)
                                       && (si.TargetId == serve.ServerID)
                                   && (si.Fixed != true || si.Fixed == null)
                                       && (si.IssueType.Fixable == true)
                                       && (AutoFixControl.High == si.IssueType.Severity || AutoFixControl.Mid == si.IssueType.Severity || AutoFixControl.Low == si.IssueType.Severity )
                                       && AutoFixControl.databaseId  == -1
                                       select si;
                    foreach (var si in serverissues)
                    {


                        IssueViewModel serverissuenode = new IssueViewModel(si.Description, new SolidColorBrush(Colors.Black), si);
                        servernode.Children.Add(serverissuenode);
                    }
                    foreach (var servi in services)
                    {


                        IssueViewModel servicenode = new IssueViewModel("Service: " + servi.InstanceName, new SolidColorBrush(Colors.Blue));
                        if (AutoFixControl.expandlevel > 0)
                        {
                            servicenode.IsNodeExpanded = true;
                        }
              

                        var databases = from i in servi.Databases
                                        where (AutoFixControl.databaseId == i.DatabaseID || AutoFixControl.databaseId == -1)
                                        select i;

                        var serviceissues = from si in dc.Issues
                                            where (si.IssueType.Scope.ScopeId == 2)
                                            && (si.TargetId == servi.ServiceID)
                                          && (si.Fixed != true || si.Fixed == null)
                                       && (si.IssueType.Fixable == true)
                                         && (AutoFixControl.High == si.IssueType.Severity || AutoFixControl.Mid == si.IssueType.Severity || AutoFixControl.Low == si.IssueType.Severity) 
                                         && AutoFixControl.databaseId == -1
                                            select si;
                        foreach (var si in serviceissues)
                        {


                            IssueViewModel serviceissuenode = new IssueViewModel(si.Description, new SolidColorBrush(Colors.Black), si);
                            servicenode.Children.Add(serviceissuenode);
                        } foreach (var db in databases)
                        {


                            IssueViewModel databasenode = new IssueViewModel("Database: " + db.DatabaseName, new SolidColorBrush(Colors.Blue));
                            if (AutoFixControl.expandlevel > 1)
                            {
                                databasenode.IsNodeExpanded = true;
                            }
                            
                            var databaseissues = from dbi in dc.Issues
                                                 where (dbi.IssueType.Scope.ScopeId == 3)
                                                 &&
                                                 (dbi.TargetId == db.DatabaseID)
                                               && (dbi.Fixed != true || dbi.Fixed == null)
                                       && (dbi.IssueType.Fixable == true)
                                   && (AutoFixControl.High == dbi.IssueType.Severity || AutoFixControl.Mid == dbi.IssueType.Severity || AutoFixControl.Low == dbi.IssueType.Severity)
                                                 select dbi;
                            foreach (var dbi in databaseissues)
                            {
                                IssueViewModel databaseissuenode = new IssueViewModel(dbi.Description, new SolidColorBrush(Colors.Black), dbi);
                                databasenode.Children.Add(databaseissuenode);
                            }

                            if (databasenode.Children.Count() != 0)
                                servicenode.Children.Add(databasenode);

                        }
                        if (servicenode.Children.Count() != 0)
                            servernode.Children.Add(servicenode);

                    }
                    if (servernode.Children.Count() != 0)
                        root.Children.Add(servernode);
                }


             

            }
            catch (System.Exception ex)
            {
              
                Logger.Error(ex);
            }
           
                root.Initialize();
                return new List<IssueViewModel> { root };
               
            
        }

        IssueViewModel(string text, SolidColorBrush color)
        {
            // this.issue = issue;
            this.Name = text;

            this.Children = new List<IssueViewModel>();
            this.kleur = color;
        }
        IssueViewModel(string text, SolidColorBrush kleur, Issue issue)
        {
            this.Issue = issue;
            this.Name = text;

            this.Children = new List<IssueViewModel>();
            this.kleur = kleur;
        }

        void Initialize()
        {
            foreach (IssueViewModel child in this.Children)
            {
                child._parent = this;
                child.Initialize();
            }
        }

        #endregion // CreateFoos

        #region Properties

        public List<IssueViewModel> Children { get; private set; }

        public bool IsInitiallySelected { get; private set; }

        public string Name { get; private set; }
        public Issue Issue { get; private set; }
        public bool IsNodeExpanded { get; private set; }
        public SolidColorBrush kleur { get;  private set; }

        #region IsChecked

        /// <summary>
        /// Gets/sets the state of the associated UI toggle (ex. CheckBox).
        /// The return value is calculated based on the check state of all
        /// child FooViewModels.  Setting this property to true or false
        /// will set all children to the same check state, and setting it 
        /// to any value will cause the parent to verify its check state.
        /// </summary>
        public bool? IsChecked
        {
            get { return _isChecked; }
            set { this.SetIsChecked(value, true, true); }
        }

        void SetIsChecked(bool? value, bool updateChildren, bool updateParent)
        {
            if (value == _isChecked)
                return;

            _isChecked = value;

            if (updateChildren && _isChecked.HasValue)
                this.Children.ForEach(c => c.SetIsChecked(_isChecked, true, false));

            if (updateParent && _parent != null)
                _parent.VerifyCheckState();

            this.OnPropertyChanged("IsChecked");
        }

        void VerifyCheckState()
        {
            bool? state = null;
            for (int i = 0; i < this.Children.Count; ++i)
            {
                bool? current = this.Children[i].IsChecked;
                if (i == 0)
                {
                    state = current;
                }
                else if (state != current)
                {
                    state = null;
                    break;
                }
            }
            this.SetIsChecked(state, false, true);
        }

        #endregion // IsChecked

        #endregion // Properties

        #region INotifyPropertyChanged Members

        void OnPropertyChanged(string prop)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion


    }
}